package com.hui.web;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * Created by yhq on 17-3-28.
 */

@RestController
@SuppressWarnings("unused")
public class TokenWeb {

    @GetMapping
    public String hello(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("TokenWeb.hello" + request.toString());
        String TOKEN = "yhqtoken";

        // 微信加密签名
        String signature = request.getParameter("signature");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");

        String[] str = {TOKEN, timestamp, nonce};
        Arrays.sort(str); // 字典序排序
        String bigStr = str[0] + str[1] + str[2];
        // SHA1加密
        String digest = DigestUtils.sha1(bigStr.getBytes()).toString().toLowerCase();
        return echostr;
    }
}
